package com.liyunc.socket.callback;

import com.liyunc.socket.callback.utils.ClientUtil;

import java.util.HashMap;
import java.util.Map;

public class Main {

    private ClientUtil.CallBack getUserInfo;

    /**
     * https://www.cnblogs.com/jerryshao2015/p/4419658.html
     * 观察者模式与回调机制的关系：我们可以把回调机制是观察者模式的一个特殊类型，即只有一个观察者。
     **/
    public static void main(String[] args) {
        Main main = new Main();
        main.initCallBack();
        main.getUserInfoFromServer();
    }

    public void getUserInfoFromServer() {
        Map<String, String> data = new HashMap<>();
        data.put("username", "tom");
        data.put("password", "tom");

        System.err.println("request//: " + data);
        ClientUtil.getInstance().request(
            data.toString(),
            getUserInfo,
            "1");
    }

    private void initCallBack() {
        getUserInfo = new ClientUtil.CallBack() {
            @Override
            public void callback(String response) {
                System.err.println("response//: " + response);
            }
        };
    }
}
